package com.tajiang.business.businesscount.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tajiang.business.R;
import com.tajiang.business.common.application.TJApp;
import com.tajiang.business.common.utils.BottomCallBackInterface;
import com.tajiang.business.common.utils.CashUtils;
import com.tajiang.business.model.GoodsDetailInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 订单适配器
 * Created by Administrator on 2017-07-27.
 */
public class GoodsGovernAdapter extends RecyclerView.Adapter<GoodsGovernAdapter.MyViewHolder> {

    private Context context;
    private List<GoodsDetailInfo> dataList;

    private BottomCallBackInterface.OnBottomClickListener mListener;

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new GoodsGovernAdapter.MyViewHolder(LayoutInflater
                .from(parent.getContext())
                .inflate(R.layout.item_goods_govern, null));
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        GoodsDetailInfo info = dataList.get(position);

        holder.name.setText(info.getShortName() + "(" +  info.getGoodsName() + ")");
        holder.fee.setText(CashUtils.changeF2Y(info.getSellPrice(), -1));
        TJApp.getImageLoader().displayImage(info.getGoodsImage(), holder.icon, TJApp.getOptionsErrorPic());

        if(info.getGoodsState() == 0){
            holder.btn.setText("上架");
        }else{
            holder.btn.setText("下架");
        }

        if(info.isChecked()){
            holder.check.setBackgroundResource(R.drawable.order_select);
        }else{
            holder.check.setBackgroundResource(R.drawable.order_unselected);
        }

        holder.check.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(mListener != null){
                    mListener.onBottomClick(v, position);
                }
            }
        });

        holder.btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(mListener != null){
                    mListener.onBottomClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public GoodsGovernAdapter(Context context, List<GoodsDetailInfo> dataList, BottomCallBackInterface.OnBottomClickListener listener) {
        this.context = context;
        this.dataList = dataList;
        this.mListener = listener;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.item_goods_govern_sel)
        ImageView check;

        @BindView(R.id.item_goods_govern_pic)
        ImageView icon;

        @BindView(R.id.item_goods_govern_name)
        TextView name;

        @BindView(R.id.item_goods_govern_fee)
        TextView fee;

        @BindView(R.id.item_goods_govern_btn)
        Button btn;

        public MyViewHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this, itemView);
        }
    }

}
